import {
	getToken,
	logout
} from 'api/user';
const TOKEN_KEY = 'token';
const USER_INFO_KEY = 'user-info';
const USER_ID = 'user-id';
const USER_COMMUNITY = 'user-community';
const TOKEN_EXPIRE_KEY = 'token-expire'; //过期时间戳
export default {
	namespaced: true,
	state: () => ({
		// 用户 token
		token: uni.getStorageSync(TOKEN_KEY) || '',
		// 用户信息
		userInfo: uni.getStorageSync(USER_INFO_KEY) || {},
		// 用户id
		userId: uni.getStorageSync(USER_ID) || '-1',
		// 用户小区信息
		userCommunity: uni.getStorageSync(USER_COMMUNITY) || '-1',
		// 过期时间戳
		tokenExpire: uni.getStorageSync(TOKEN_EXPIRE_KEY) || ''
	}),
	mutations: {

		//-- 保存 token 到 vuex
		setToken(state, token) {
			state.token = token;
			this.commit('user/saveToken');
		},
		// 保存 token 到本地存储
		saveToken(state) {
			uni.setStorage({
				key: TOKEN_KEY,
				data: state.token
			})
		},
		// 删除 token
		removeToken(state) {
			state.token = '';
			this.commit('user/saveToken');
		},



		//-- 保存 userInfo 到 vuex
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo;
			this.commit('user/saveUserInfo');
		},
		// 保存 userInfo 到本地存储
		saveUserInfo(state) {
			uni.setStorage({
				key: USER_INFO_KEY,
				data: state.userInfo
			})
		},
		// 删除 userInfo
		removeUserInfo(state) {
			state.userInfo = {};
			this.commit('user/saveUserInfo');
		},


		//-- 保存 userId 到 vuex
		setUserId(state, userId) {
			state.userId = userId;
			this.commit('user/saveUserId');
		},
		// 保存 userId 到本地存储
		saveUserId(state) {
			uni.setStorage({
				key: USER_ID,
				data: state.userId
			})
		},
		// 删除 userId
		removeUserId(state) {
			state.userId = '';
			this.commit('user/saveUserId');
		},


		//-- 保存 userCommunity 到 vuex
		setUserCommunity(state, userCommunity) {
			state.userCommunity = userCommunity;
			this.commit('user/saveUserCommunity');
		},
		// 保存 userCommunity 到本地存储
		saveUserCommunity(state) {
			uni.setStorage({
				key: USER_COMMUNITY,
				data: state.userCommunity
			})
		},
		// 删除 userCommunity
		removeUserCommunity(state) {
			state.userCommunity = '';
			this.commit('user/saveUserCommunity');
		},


		//-- 保存 tokenExpire 到 vuex
		setTokenExpire(state, tokenExpire) {
			state.tokenExpire = tokenExpire;
			this.commit('user/saveTokenExpire');
		},
		// 保存 tokenExpire 到本地存储
		saveTokenExpire(state) {
			uni.setStorage({
				key: TOKEN_EXPIRE_KEY,
				data: state.tokenExpire
			})
		},
		// 删除 tokenExpire
		removeTokenExpire(state) {
			state.tokenExpire = '';
			this.commit('user/saveTokenExpire');
		},
	},
	// 在 actions 中完成异步操作
	actions: {
		/**
		 * 保存微信登录信息
		 */
		saveUserInfo(context, data) {
			console.log('saveWxLogin', data)
			this.commit('user/setToken', data.token);
			this.commit('user/setUserId', data.userInfo.userId); // 用户id
			this.commit('user/setUserInfo', data.userInfo);
			this.commit('user/setTokenExpire', data.tokenExpire); //过期时间戳
		},
		/**
		 * 保存当前的小区
		 */
		saveUserCommunity(context, data) {
			this.commit('user/setUserCommunity', data);
		},
		/**
		 * 退出登录,清除数据
		 */
		cleanUserInfo(context) {
			let token = {
				token: context.state.token
			}
			logout(token)
			this.commit('user/removeToken');
			this.commit('user/removeUserInfo');
			this.commit('user/removeUserId');
			this.commit('user/removeUserCommunity')
			this.commit('user/removeTokenExpire'); //过期时间戳
		}
	}
}
